package pl.us.ij.plugins.segmentation.forms;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.watershed.OCCOCommand;
import pl.us.ij.plugins.segmentation.commands.watershed.OCCOCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-20
 * Time: 23:11:56
 * To change this template use File | Settings | File Templates.
 */
public class OpenCloseForm extends CategoryInfo {
    private JPanel panel1;
    private JButton OCCOButton;
    private JComboBox occoMaskSizeCombobox;

    public OpenCloseForm(IActionHandler actionHandler) {
        actionHandler.setAction(OCCOButton, OCCOCommand.class, getOCCOParams());
    }

    @Override
    public String getTitle() {
        return Localization.getString("openCloseForm.title");
    }

    @Override
    public JPanel getPanel() {
        return panel1;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private ICommandParamGather getOCCOParams() {
        return new ICommandParamGather() {

            public CommandParams getCommandParams() {
                OCCOCommandParams params = new OCCOCommandParams();
                params.setMaskSize(Integer.parseInt(occoMaskSizeCombobox.getSelectedItem().toString()));

                return params;
            }
        };
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:grow", "fill:d:grow"));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FormLayout("fill:115px:noGrow,left:4dlu:noGrow,fill:67px:noGrow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(panel2, cc.xy(1, 1));
        OCCOButton = new JButton();
        this.$$$loadButtonText$$$(OCCOButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("openCloseForm.OCCO"));
        OCCOButton.setToolTipText("");
        panel2.add(OCCOButton, cc.xyw(1, 3, 3));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("openCloseForm.maskSizeLabel"));
        panel2.add(label1, cc.xy(1, 1));
        occoMaskSizeCombobox = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("3");
        defaultComboBoxModel1.addElement("5");
        defaultComboBoxModel1.addElement("7");
        defaultComboBoxModel1.addElement("9");
        defaultComboBoxModel1.addElement("11");
        defaultComboBoxModel1.addElement("13");
        defaultComboBoxModel1.addElement("15");
        defaultComboBoxModel1.addElement("17");
        defaultComboBoxModel1.addElement("19");
        occoMaskSizeCombobox.setModel(defaultComboBoxModel1);
        panel2.add(occoMaskSizeCombobox, cc.xy(3, 1));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
